package org.example.tree.binary;

import org.example.tree.entity.ArrayBinaryNode;

/**
 * @author zjt
 * @description
 * @date 2024/1/31 14:26
 */
public class ArrayBinaryTraversal {

    int arr[];

    /**
     * 数组和树的相互转换遍历
     *
     * @param arrayBinaryNode
     */
    public void preorderTraversal(ArrayBinaryNode arrayBinaryNode) {
        arr = arrayBinaryNode.arr;
        preorderTraversal(0);
    }

    /**
     * 数组前序遍历
     *
     * @param index
     */
    public void preorderTraversal(int index) {
        if (arr == null || arr.length == 0) {
            System.out.println("数组为空");
        }
        System.out.println(arr[index]);
        if ((index * 2 + 1) < arr.length) {
            preorderTraversal(index * 2 + 1);
        }
        if ((index * 2 + 2) < arr.length) {
            preorderTraversal(index * 2 + 2);
        }
    }
}
